'use strict';
const db = uniCloud.database()

exports.main = async (event, context) => {
	try {
		const { lastId } = event
		const collection = db.collection('saigedata')
		let query = collection.orderBy('lastRank', 'asc')
		
		// 如果有lastId，则从该ID之后开始查询
		if (lastId) {
			const lastRecord = await collection.doc(lastId).get()
			if (lastRecord.data && lastRecord.data[0]) {
				query = query.where({
					lastRank: db.command.gt(lastRecord.data[0].lastRank)
				})
			}
		}
		
		const res = await query.limit(20).get()
		
		return {
			code: 0,
			data: res.data,
			msg: '获取数据成功'
		}
	} catch (e) {
		return {
			code: -1,
			data: null,
			msg: '获取数据失败：' + e.message
		}
	}
} 